﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace RN.ERP.Web
{
    public class ZbdlryDBHelper
    {
        public static DataSet GetDataTable(string sql,SqlParameter[] parameters)
        {
            try
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ZbdlryConnectionString"].ConnectionString);
                conn.Open();
                using (conn)
                {
                    DataSet ds = new DataSet();
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    if (parameters != null && parameters.Length > 0)
                    {
                        cmd.Parameters.AddRange(parameters);
                    }
                    SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
                    dataAdapter.Fill(ds);
                    return ds;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static DataSet GetDataTable(string sql, SqlParameter[] parameters, int startRecord, int maxRecord,out int count)
        {
            try
            {
                count = 0;
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ZbdlryConnectionString"].ConnectionString);
                conn.Open();
                using (conn)
                {
                    DataSet ds = new DataSet();
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    if (parameters != null && parameters.Length > 0)
                    {
                        cmd.Parameters.AddRange(parameters);
                    }
                    SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
                    dataAdapter.Fill(ds);
                    if (ds != null & ds.Tables.Count > 0)
                    {
                        count = ds.Tables[0].Rows.Count;
                    }
                    DataSet currentDs = new DataSet();
                    dataAdapter.Fill(currentDs, startRecord, maxRecord, "table");
                    return currentDs;
                }
            }
            catch (Exception ex)
            {
                count = 0;
                return null;
            }
        }

        public static object GetObject(string sql, SqlParameter[] parameters)
        {
            try
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ZbdlryConnectionString"].ConnectionString);
                conn.Open();
                using (conn)
                {
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    if (parameters != null && parameters.Length > 0)
                    {
                        cmd.Parameters.AddRange(parameters);
                    }
                    var obj = cmd.ExecuteScalar();
                    return obj;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}